<template>
  <div class="time-select">
    <div class="time-select-main">
      <div class="back-today" @click="backToday">今</div>
      <div class="year-show" @click="monshow = !monshow">
        {{yearShow}}年{{monthShow}}月
        <p :class="[monshow?roateActive:roateFade]"><svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-diary-jiantou"></use>
            </svg></p>
      </div>
      <transition name="slide-fade">
        <div v-show="monshow" class="month-select">
          <div class="year-select">
            <p class="left" @click="minus"><svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-diary-jiantou"></use>
            </svg></p>
            <p class="center" :class="[numShake?numShakeActive:numShakeFade]" ref="yearcenter">{{yearChange}}</p>
            <p class="right" @click="add"><svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-diary-jiantou"></use>
            </svg></p>
          </div>
          <ul>
            <li v-for="(item,index) in monthList" :class="{active:index===monthShow-1}" @click="monthTap(index)" :key="index">
              {{item}}月
            </li>
          </ul>
        </div>
      </transition>
      <div class="week">
        <ul>
          <li>周一</li><li>周二</li><li>周三</li><li>周四</li><li>周五</li><li class="weekend">周六</li><li class="weekend">周日</li>
        </ul>
      </div>
      <div class="swiper-container" id="date-select">
        <div class="swiper-wrapper">
          <!-- It is important to set "left" style prop on every slide -->
          <div class="swiper-slide" :class="[{active:slide === activeList},{activetip:activeTipList.indexOf(slide)!=-1}]"
              v-for="(slide, index) in virtualData.slides"
              :key="index"
              :style="{left: `${virtualData.offset}px`}"
            ><p>{{slide | getLastNum}}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// @ is an alias to /src
// import autoPrefix from "@/assets/js/common/autoprefix";
import Swiper from "@/assets/js/common/swiper.esm.bundle";
import "@/assets/css/animate.css";
export default {
  name: "ArticleGuanZhu",
  data() {
    /* eslint-disable */
    return {
      lists: "",
      start: "",
      current: "",
      monshow: false,
      yearChange: "",
      yearShow: "",
      monthShow: "",
      swiperTimeSelect: "",
      numShake: false,
      roateActive: "roate-active",
      roateFade: "roate-fade",
      numShakeActive: "",
      numShakeFade: "num-shake-fade",
      monthList: ["一","二","三","四","五","六","七","八","九","十","十一","十二"],
      activeList: "",
      activeTipList: ["20181005","20181006","201811010","20181112","20181205","20181206","20181207","20181215","20181218","20181219","20191223"],
      slides: (() => {
        let start = Date.parse(new Date("1, 3, 2006"));
        let end = Date.parse(new Date("12, 31, 2040"));
        let perDay = 1000 * 60 * 60 * 24;
        let days = [];
        for (let i = start / perDay; i < end / perDay; i++) {
          let day = new Date(i * perDay).getDate();
          day = day < 10 ? 0 + "" + day:day;
          let month = new Date(i * perDay).getMonth() + 1;
          month = month < 10 ? 0 + "" + month : month;
          let year = new Date(i * perDay).getFullYear();
          days.push(year + "" + month + "" + day);
        }
        return days;
      })(),
      /* eslint-enable */
      // virtual data
      virtualData: {
        slides: []
      }
    };
  },
  components: {},
  computed: {},
  filters: {
    getLastNum: function(value) {
      if (!value) return "";
      value = value.toString();
      return value.slice(6, 8);
    }
  },
  methods: {
    touchSlide() {
      // let listLi = document.querySelectorAll(id + " li");
      // for (let i = 0; i < listLi.length; i++) {
      //   listLi[i].addEventListener("touchstart", touch, false);
      //   listLi[i].addEventListener("touchmove", touch, false);
      //   listLi[i].addEventListener("touchend", touch, false);
      // }
      // let transform = autoPrefix("transform")[1];
      // let transition = autoPrefix("transition")[1];
      // // let _self = this;
      // let w = document.body.clientWidth || document.documentElement.clientWidth;
      // function touch(event) {
      //   let currentX;
      //   switch (event.type) {
      //     case "touchstart":
      //       {
      //         var st = window.getComputedStyle(this, null);
      //         let tr =
      //           st.getPropertyValue("-webkit-transform") ||
      //           st.getPropertyValue("-moz-transform") ||
      //           st.getPropertyValue("-ms-transform") ||
      //           st.getPropertyValue("-o-transform") ||
      //           st.getPropertyValue("transform") ||
      //           "FAIL";
      //         let values = tr
      //           .split("(")[1]
      //           .split(")")[0]
      //           .split(",");
      //         this.transltedX = values[4];
      //         this.startX = event.touches[0].clientX;
      //       }
      //       break;
      //     case "touchmove":
      //       currentX = event.touches[0].clientX;
      //       this.driftX = currentX - this.startX;
      //       this.style[transform] =
      //         "translate3d(" +
      //         (Number(this.transltedX) + this.driftX) +
      //         "px,0,0)";
      //       if (this.driftX > 0) {
      //         // console.log(Number(this.transltedX) + w + this.driftX);
      //         $(this).prev()[0].style[transform] =
      //           "translate3d(" +
      //           (Number(this.transltedX) - w + this.driftX) +
      //           "px,0,0)";
      //       } else {
      //         $(this).next()[0].style[transform] =
      //           "translate3d(" +
      //           (Number(this.transltedX) + w + this.driftX) +
      //           "px,0,0)";
      //       }
      //       break;
      //     case "touchend":
      //       console.log(3);
      //       console.log(this.driftX);
      //       if (this.driftX < 80 && this.driftX > 0) {
      //         this.style[transition] = "transform .2s ease-out";
      //         this.style[transform] = "translate3d(100%,0,0)";
      //         $(this).prev()[0].style[transition] = "transform .2s ease-out";
      //         $(this).prev()[0].style[transform] = "translate3d(0,0,0)";
      //       } else if (this.driftX > 80) {
      //         this.style[transition] = "transform .2s ease-out";
      //         this.style[transform] = "translate3d(200%,0,0)";
      //         $(this).prev()[0].style[transition] = "transform .2s ease-out";
      //         $(this).prev()[0].style[transform] = "translate3d(100%,0,0)";
      //       } else if (this.driftX > -80 && this.driftX < 0) {
      //         this.style[transition] = "transform .2s ease-out";
      //         this.style[transform] = "translate3d(100%,0,0)";
      //         $(this).next()[0].style[transition] = "transform .2s ease-out";
      //         $(this).next()[0].style[transform] = "translate3d(200%,0,0)";
      //       } else {
      //         this.style[transition] = "transform .2s ease-out";
      //         this.style[transform] = "translate3d(0,0,0)";
      //         $(this).next()[0].style[transition] = "transform .2s ease-out";
      //         $(this).next()[0].style[transform] = "translate3d(100%,0,0)";
      //       }
      //       this.removeEventListener("touchmove", this, false);
      //       this.removeEventListener("touchend", this, false);
      //       break;
      //   }
      // }
    },
    minus() {
      this.numShake = true;
      this.numShakeActive = "num-shake-active-l";
      setTimeout(() => {
        this.yearChange--;
        this.numShake = false;
      }, 300);
    },
    add() {
      this.numShake = true;
      this.numShakeActive = "num-shake-active-r";
      setTimeout(() => {
        this.yearChange++;
        this.numShake = false;
      }, 300);
    },
    monthTap(index) {
      let chosenDate = Date.parse(
        new Date(index + 1 + ", 15, " + this.yearChange)
      );
      this.swiperTimeSelect.slideTo(
        (chosenDate - this.start) / 1000 / 60 / 60 / 24
      );
      this.monshow = false;
    },
    backToday() {
      this.swiperTimeSelect.slideTo(
        (this.current - this.start) / 1000 / 60 / 60 / 24
      );
      this.monshow = false;
    },
    getCoupleTime(time) {
      return time < 10 && time > 0 ? 0 + "" + time : time;
    }
  },
  mounted() {
    const self = this;
    this.current = Date.now();
    let currentMsg = new Date(this.current);
    this.yearChange = currentMsg.getFullYear();
    this.activeList =
      currentMsg.getFullYear() +
      "" +
      this.getCoupleTime(currentMsg.getMonth() + 1) +
      "" +
      this.getCoupleTime(currentMsg.getDate());
    // console.log(this.activeList);
    this.start = Date.parse(new Date("1, 3, 2006"));
    // console.log(this.yearChange);
    this.swiperTimeSelect = new Swiper("#date-select", {
      virtual: {
        slides: self.slides,
        renderExternal(data) {
          // assign virtual slides data
          self.virtualData = data;
        }
      },
      slidesPerView: 7,
      slidesPerGroup: 7,
      speed: 500,
      longSwipesRatio: 0.1,
      on: {
        slideChangeTransitionEnd: function() {
          let ts = this.activeIndex * 1000 * 60 * 60 * 24 + self.start;
          self.yearShow = new Date(ts).getFullYear();
          self.monthShow = new Date(ts).getMonth() + 1;
        }
      }
    });
    this.backToday();
  }
};
</script>
<style scoped lang="scss">
.time-select {
  width: 100%;
  padding-left: r(16);
  padding-right: r(16);
  box-sizing: border-box;
  padding-top: r(24);
  .time-select-main {
    width: 100%;
    border-radius: r(18);
    padding-left: r(16);
    padding-right: r(16);
    box-sizing: border-box;
    background: #66a8da url("../../assets/img/time-select-bg.png") center
      no-repeat;
    background-size: 100%;
    position: relative;
    .back-today {
      position: absolute;
      width: r(40);
      height: r(40);
      right: r(24);
      top: r(24);
      color: #004a80;
      font-size: r(24);
      text-align: center;
      line-height: r(45);
      border-radius: 100px;
      background: rgba($color: #fff, $alpha: 0.4);
    }
    .month-select {
      width: 100%;
      overflow: hidden;
      height: r(220);
      .year-select {
        width: 100%;
        height: r(48);
        line-height: r(48);
        border: 1px solid rgba($color: #fff, $alpha: 0.1);
        border-radius: r(12);
        margin-bottom: r(12);
        font-size: r(28);
        font-weight: bold;
        color: #fff;
        box-sizing: border-box;
        p {
          display: block;
          float: left;
          &.left {
            width: 10%;
            transform: rotateZ(90deg);
          }
          &.center {
            width: 80%;
          }
          &.right {
            width: 10%;
            transform: rotateZ(270deg);
          }
        }
      }
      ul {
        display: block;
        overflow: hidden;
        li {
          width: 25%;
          color: #fff;
          font-size: r(28);
          padding: r(12);
          box-sizing: border-box;
          float: left;
          opacity: 0.8;
          border-radius: r(12);
          &.active {
            background: rgba($color: #fff, $alpha: 0.4);
            color: #004a80;
          }
        }
      }
    }
    .slide-fade-enter-active,
    .slide-fade-leave-active {
      transition: all 0.3s ease;
    }
    .slide-fade-enter,
    .slide-fade-leave-to {
      height: 0;
      opacity: 0;
    }
    .slide-fade-enter-to,
    .slide-fade-leave {
      height: r(220);
      opacity: 1;
    }
    .year-show {
      width: 100%;
      padding-top: r(48);
      height: r(68);
      line-height: r(68);
      font-size: r(28);
      color: #fff;
      font-weight: bold;
      p {
        font-size: r(32);
        line-height: r(32);
        display: inline-block;
      }
    }
    .week {
      width: 100%;
      height: r(60);
      line-height: r(60);
      padding-top: r(12);
      ul li {
        width: 14.28%;
        color: #fff;
        font-size: r(28);
        float: left;
      }
      .weekend {
        color: #ffe52d;
      }
    }
  }
  ul {
    // width: 100%;
    // height: 100%;
    // position: relative;
    // transform: translate3d(-100%, 0, 0);
    // -webkit-transform: translate3d(-100%, 0, 0);
    // li {
    //   position: absolute;
    //   width: 100%;
    //   height: 100%;
    //   top: 0;
    //   left: 0;
    //   background: #ccc;
    //   &:first-child {
    //     transform: translate3d(0, 0, 0);
    //     -webkit-transform: translate3d(0, 0, 0);
    //   }
    //   &:nth-child(2) {
    //     transform: translate3d(100%, 0, 0);
    //     -webkit-transform: translate3d(100%, 0, 0);
    //   }
    //   &:last-child {
    //     transform: translate3d(200%, 0, 0);
    //     -webkit-transform: translate3d(200%, 0, 0);
    //   }
    //   p {
    //     display: block;
    //     width: 14.2859%;
    //     float: left;
    //     font-size: r(32);
    //     line-height: r(200);
    //     font-weight: bold;
    //   }
    // }
  }
}
.swiper-container {
  width: 100%;
  height: r(160);
  .swiper-wrapper {
    width: 100%;
    height: 100%;
    .swiper-slide {
      width: 14.28%;
      font-size: r(32);
      color: #fff;
      margin-top: r(10);
      line-height: r(100);
      p {
        width: r(48);
        height: r(48);
        display: inline-block;
        text-align: center;
        line-height: r(50);
        text-indent: 0;
        box-align: border-box;
        border-radius: 200px;
      }
    }
    .active {
      color: #004a80;
      p {
        background: rgba($color: #fff, $alpha: 0.6);
      }
    }
    .activetip {
      color: #fff;
      p {
        background: none;
        border: 1px solid rgba($color: #fff, $alpha: 0.4);
        box-sizing: border-box;
        position: relative;
        &::after {
          content: "";
          position: absolute;
          top: r(70);
          width: 6px;
          display: block;
          height: 6px;
          border-radius: 6px;
          left: r(17);
          opacity: 0.5;
          background: #fff;
        }
      }
    }
  }
}
.roate-active,
.roate-fade,
.num-shake-active-l,
.num-shake-active-r,
.num-shake-fade {
  transition: all 0.3s ease;
}
.roate-active {
  transform: rotateZ(180deg);
}
.roate-fade {
  transform: rotateZ(0deg);
}
.num-shake-active-l {
  transform: translateX(-10px);
  opacity: 0;
}
.num-shake-active-r {
  transform: translateX(10px);
  opacity: 0;
}
.num-shake-fade {
  transform: translateX(0);
  opacity: 1;
}
</style>
